package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.HotelComment;
import cn.wolfcode.luowowo.comment.domain.HotelComment;
import cn.wolfcode.luowowo.comment.query.HotelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.HotelCommentRepository;
import cn.wolfcode.luowowo.comment.service.IHotelCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class HotelCommentServiceImpl implements IHotelCommentService{

    @Autowired
    private HotelCommentRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public Page query(HotelCommentQuery qo) {
        //1.创建查询对象
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("hotelId").is(qo.getHotelId());
        query.addCriteria(criteria);
        //3.查询总条数
        long count = template.count(query, HotelComment.class);
        //long count = repository.count(query, HotelComment.class);------>这个方法不行
        //4.封装分页查询对象
        //Pageable--->拿它的实现类
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(),
                Sort.Direction.DESC,
                "createTime");
        query.with(pageRequest);
        //5.查询list
        List<HotelComment> list = template.find(query, HotelComment.class);
        //返回Page实现类
        //PageImpl(List<T> content, Pageable pageable, long total)
        return new PageImpl(list,pageRequest,count);
    }

    @Override
    public HotelComment commentAdd(HotelComment comment) {
        //判断id是否存在
        Long commentCount;
        if(!StringUtils.hasLength(comment.getId())){
            comment.setCreateTime(new Date());
        }
        //判断引用评论
        if(comment.getType()==HotelComment.TRAVLE_COMMENT_TYPE){
            if(StringUtils.hasLength(comment.getRefComment().getId())){
                comment.setRefComment(repository.findById(comment.getRefComment().getId()).get());
                HotelComment refComment = comment.getRefComment();
                if (refComment.getCommentCount()==null){
                     commentCount = 0L;
                }else {
                        commentCount = refComment.getCommentCount();
                }
                commentCount++;
                refComment.setCommentCount(commentCount);
                repository.save(refComment);
            }
        }
        repository.save(comment);
        return comment;
    }

    @Override
    public Long count(Long hotelId) {
        //2.查询总条数
        Query query = new Query();
        //2.封装条件查询对象
        Criteria criteria = Criteria.where("hotelId").is(hotelId);
        query.addCriteria(criteria);
        long count = template.count(query, HotelComment.class);
        return count;
    }

    @Override
    public List<HotelComment> getHotelCommentByUserId(Long id) {
//        Criteria criteria1 = new Criteria();
//        criteria1.where("userId").regex(id.toString());
//        Query query = new Query();
//        query.addCriteria(criteria1);
//        List<HotelComment> list = template.find(query, HotelComment.class);
        List<HotelComment> list = repository.findByUserId(id);
        return list;
    }

}
